//-----------------------------------------------------------------------------
//
// Filename: RequestState.cs
//
// Description: A state class used by related assemblies to access
// information from an incoming HTTP request.
//
// History:
// 12 Jan 2004	Aaron Clauson	Created.
// 04 Feb 2004	Aaron Clauson	Added Populator method.
//
// License: 
// Public Domain.
//-----------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web;

namespace MaverickLite.Config
{
	public class RequestState
	{
		public HttpContext m_inContext;	// Holds a reference to the current HTTP context.

		public string RequestIPAddress = null;	// The IP address the request was received from.
		public string Path = null;				// The relative path of the current request.

		public RequestState(HttpContext inContext)
		{
			this.m_inContext = inContext;
			RequestIPAddress = inContext.Request.UserHostAddress;
			Path = inContext.Request.Path;
		}

		/// <summary>
		/// Attempts to extract a value field for a specific key from a HTTP request.
		/// </summary>
		/// <param name="key">Key to extract value for.</param>
		/// <returns>Value of the request key field.</returns>
		public string GetRequestValue(string key)
		{
			if(this.m_inContext.Request.QueryString[key] != null && this.m_inContext.Request.QueryString[key].Trim() != String.Empty)
			{
				return this.m_inContext.Request.QueryString[key];
			}
			else if (this.m_inContext.Request.Form[key] != null && this.m_inContext.Request.Form[key].Trim() != String.Empty)
			{
				return this.m_inContext.Request.Form[key];
			}

			return null;
		}

		/// <summary>
		/// Attempts to get the value from a cookie sent in an Http request.
		/// </summary>
		/// <param name="cookieName">The name of the cookie to retrieve the value for.</param>
		/// <returns>If the cookie exists returns its value otherwise null.</returns>
		public string GetCookieValue(string cookieName)
		{
			string cookieValue = null;

			if(this.m_inContext.Request.Cookies.Count > 0)
			{
				HttpCookie cookie = this.m_inContext.Request.Cookies.Get(cookieName);
		
				if(cookie != null)
				{
					return cookie.Value;
				}
			}

			return cookieValue;
		}

		/// <summary>
		/// Returns a combination of the data in the request querystring and form. The two data
		/// sources are combined to produce a single hashtable. The data in the querystring
		/// takes precedence if there is a conflict.
		/// </summary>
		/// <returns>A hashtable containing the amalgamated data.</returns>
		public Hashtable GetRequestData()
		{
			NameValueCollection formData = this.m_inContext.Request.Form;
			NameValueCollection queryStringData = this.m_inContext.Request.QueryString;

			Hashtable data = new Hashtable();	// Collection that will be populated with request data and returned.

			// Copy request querystring data.
			if(queryStringData.Count > 0)
			{			 
				IEnumerator queryStringEnum = queryStringData.GetEnumerator();
				while(queryStringEnum.MoveNext())
				{
					string key = (string)queryStringEnum.Current;
					data.Add(key, queryStringData[key]);
				}
			}

			// Amalgamate form data.
			if(formData.Count > 0)
			{
				IEnumerator formEnum = formData.GetEnumerator();
				while(formEnum.MoveNext())
				{
					string key = (string)formEnum.Current;

					// Querystring data takes precedence. In practice this should hardly ever
					// occur. It would be unusual for a request to contain both form and querystring
					// data let alone duplicate fields; however, better safe then sorry.
					if(!data.ContainsKey(key))
					{
						data.Add(key, formData[key]);
					}
				}
			}

			return data;
		}
	}
}
